<?php


namespace App\Http\Controllers;


use App\Facades\Tx;
use App\Models\Goods;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    public function create(Request $request)
    {
        $this->validate($request,
        [
            'name'=>'required',
            'image'=>'required',
//            'game_images'=>'required',
            'introduction' => 'required',
            'price' => 'required'
        ],
        [
            'name.*'=>'请填写商品名称',
            'image.*'=>'请上传商品主图',
            'price.*'=>'请正确填写价格',
            'game_images.*'=> '请至少上传一张游戏内截图'
        ]
        );



       return Tx::success(Goods::create([
                             'name' => trim($request->get('name')),
                             'image' => $request->get('image'),
                             'price' => trim($request->get('price')),
                             'introduction' => trim($request->get('introduction')),
                             'game_images' => $request->get('game_images')
                         ]));

    }

    public function delete(Request $request,Goods $goods)
    {
        if($goods->uid != \Auth::id())
        {
            Tx::fail('无法删除');
        }

        $goods->delete();
        return Tx::success('','删除成功');
    }

    public function show(Request $request,Goods $goods)
    {
        return Tx::success($goods);
    }

    public function index(Request $request)
    {
        $list = Goods::where('status',1)->orderBy('id','desc')->paginate();
        return Tx::success($list);
    }
    public function my(Request $request)
    {
        $db = Goods::where('uid',\Auth::id());

        if($request->get('status')&&$status=$request->get('status'))
        {
            $db = $db->where('status',$status);
        }

        $list = $db->orderBy('id','desc')->paginate();
        return Tx::success($list);
    }

    public function saveImage(Request $request)
    {
        $file = $request->file('file');
        return Tx::success($image = $request->file('image')->store('goods'));
    }
}
